package com.samba.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import org.apache.myfaces.custom.fileupload.UploadedFile;

import com.samba.entites.Category;
import com.samba.entites.Preteur;
import com.samba.entites.Projet;
import com.samba.entites.Type;
import com.samba.sessions.AdminServiceDAO;


@ManagedBean(name="projetManager")
@SessionScoped
public class ProjetManagerBean {
	
	@EJB()
	private AdminServiceDAO adminService;
	
	private Projet managed ;
	private UploadedFile logo;
	
	private Collection <Type> types;
	private Collection <Category> categories;
	private Collection <Projet> listesProjets;
	private boolean initOk;
	
	private  boolean removeLogo;
	
	

	
	
	/*
	 * create a new instance of ProetMangedBean
	 */
	
	public ProjetManagerBean()
	{
		managed = new Projet();
	}
	
	private void init ()
	{
		if(!initOk)
		{
			types = adminService.findAllTypes();
			categories = adminService.findAllCategories();
			listesProjets = adminService.findAllProjets();
			setRemoveLogo(false);
		}
	}
	
	public void initProjetFromRequestParam()
	{
		managed = adminService.findProjetById(getManagedProjetId());
	}
	public int getManagedProjetId()
	{

		ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		String param = request.getParameter("id");
		return Integer.parseInt(param);
	}
	
	public Collection getListType()
	{
		init();
		Collection listSelect = new ArrayList();
		
		Iterator<Type> it = types.iterator();
		while(it.hasNext())
		{
			Type current = it.next();
			if(current.getId() > 0)
			{
				SelectItem selectItem = new SelectItem(current, current.getLabel());
				listSelect.add(selectItem);
			}
		}
		return listSelect;
	}

	public Collection getListCategory() {
		init();
		Collection listSelect = new ArrayList();
		
		Iterator<Category> it = categories.iterator();
		while(it.hasNext())
		{
			Category current = it.next();
			if(current.getId() > 0)
			{
				SelectItem selectItem = new SelectItem(current, current.getLabel());
				listSelect.add(selectItem);
			}
		}
		return listSelect;
	}

	
	/**
	 * retourn les elements de la base de donnees
	 * @return
	 */
	public Collection getList()
	{
		init();
		return listesProjets;
	}
	public void remove()
	{
		adminService.deleteProjet(getManagedProjetId());
	}
	public void edit()
	{
		System.out.println("edit projet.....");
		initProjetFromRequestParam();
	}
	public void newBlank()
	{
		managed = new Projet();
	}
	
	public void save()
	{
		// preparation du logo
		if(getLogo() != null && getLogo().getSize() > 0)
		{
		 try
		 {
			 managed.setLogo(getLogo().getBytes());
		 }
		 catch(IOException e)
		 {
			 System.out.println("Impossible de recuper l'image ");
		 }
		}
		// ajout ou modification 
		if(managed.getId() > 0)
		{
			adminService.updateProjet(managed);
		} else
		{
		   
			//temp
			Preteur [] tabPreur = null;
			try
			{
				tabPreur =getManaged().getPreteurs().toArray(new Preteur[0]);
			}
			catch(Exception e) 
			{
				e.printStackTrace();
		    }
			 // fin temp 
			
		    managed.getPreteurs().toArray(new Preteur[0]);
		    managed = adminService.addProjet(managed);
		    newBlank(); 
		}
	
	}
	public Projet getManaged() {
		return managed;
	}

	public void setManaged(Projet managed) {
		this.managed = managed;
	}

	public UploadedFile getLogo() {
		return logo;
	}

	public void setLogo(UploadedFile logo) {
		this.logo = logo;
	}

	public Collection <Type> getTypes() {
		return types;
	}

	public void setTypes(Collection <Type> types) {
		this.types = types;
	}

	public Collection <Category> getCategories() {
		return categories;
	}

	public void setCategories(Collection <Category> categories) {
		this.categories = categories;
	}

	public boolean isInitOk() {
		return initOk;
	}

	public void setInitOk(boolean initOk) {
		this.initOk = initOk;
	}

	public boolean isRemoveLogo() {
		return removeLogo;
	}

	public void setRemoveLogo(boolean removeLogo) {
		if(removeLogo)
			// aucne immage 
		 managed.setLogo(new byte[0]);
	}

	public Collection <Projet> getListesProjets() {
		return listesProjets;
	}

	public void setListesProjets(Collection <Projet> listesProjets) {
		this.listesProjets = listesProjets;
	}

	public Preteur [] getListePreteurs() {
		Preteur [] tabPret = null;
		try
		{
			tabPret = getManaged().getPreteurs().toArray(new Preteur[0]);
			return tabPret;
		}
		catch(Exception io)
		{
			return new Preteur[0];
		}
	}

	public void setListePreteurs(Preteur [] listePreteurs) {
		for(Preteur p : listePreteurs)
		{
			getManaged().getPreteurs().add(p);
		}
	}

	
	
}
